export default class Cookie {

  private static defaultExdays = 1

  /**
   * 设置 cookie
   * @param {string} key cookie 名
   * @param {string} value cookie 值
   * @param {number} exdays 有效天数
   * @param {string} domain 有效域
   * @param {string} path 有效路径
   */
  static set(key: string, value: string, exdays = this.defaultExdays, domain = '' , path = '') {
    const
      d = new Date(new Date().getTime() + (exdays * 24 * 60 * 60 * 1000)),
      expires = "expires=" + d.toUTCString(),
      path_ = path ? "path=" + path : "path=/",
      domain_ = domain ? "domain=" + domain + ';' : ';'

    document.cookie = key + "=" + value + "; " + domain_ + expires + "; " + path_
  }

  /**
   * 获取 cookie
   * @param key cookie名
   * @returns 
   */
  static get(key: string) {
    const
      reg = new RegExp('(^| )'+ key + '=([^;]*)(;|$)'),
      arr = document.cookie.match(reg)

    return arr ? arr[2] : ''
  }

  /**
   * 删除 cookie
   * @param {string} key 需要删除的 cookie 名
   */
  static remove(key: string) {
    this.set(key, '')
  }

}
